﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UMC.Data;
using UMC.Web;
using UMC.WorkFlow.Entities;

namespace UMC.WorkFlow.Form
{

    public class FormUIGridDialog : UIGridDialog
    {
        //TaskFormControl control;
        //TaskForm form;
        IDictionary dictionary;

        public string ValueKey { get; set; }
        public string ProviderKey { get; set; }
        public string SelectSQL { get; set; }
        public FormUIGridDialog(IDictionary dictionary)
        {

            this.dictionary = dictionary;
            if (String.IsNullOrEmpty(this.SelectSQL) == false)
            {
                this.IsSearch = this.SelectSQL.IndexOf("{keyword}", StringComparison.CurrentCultureIgnoreCase) > 0;
            }
        }
        protected override Hashtable GetData(IDictionary paramsKey)
        {
            var start = UMC.Data.Utility.Parse((paramsKey["start"] ?? "0").ToString(), 0);
            var limit = UMC.Data.Utility.Parse((paramsKey["limit"] ?? "25").ToString(), 25);
            var param = new Hashtable(this.dictionary);
            param["Keyword"] = String.Format("%{0}%", paramsKey["Keyword"]);// as string ?? String.Empty);//.Split(',');

            //   int index = control.SelectSQL.IndexOf(" from ", StringComparison.CurrentCultureIgnoreCase);

            var match = System.Text.RegularExpressions.Regex.Match(this.SelectSQL, @"\sfrom\s", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            var index = match.Index;

            String countSQL = "SELECT COUNT(*) " + this.SelectSQL.Substring(index);
            var sqler = UMC.Data.Database.Instance(this.ProviderKey).Sqler(false);
            var data = sqler.Execute(this.SelectSQL, start, limit, param);
            var sel = this.SelectSQL.Substring(0, index).Split(',');
            char file = 'a';
            foreach (var s in sel)
            {
                var asindex = s.IndexOf(" as ", StringComparison.CurrentCultureIgnoreCase);
                if (asindex > 0)
                {

                    var title = s.Substring(asindex + 4).Trim();
                    if (String.Equals(this.ValueKey, title, StringComparison.CurrentCultureIgnoreCase) == false)
                    {
                        if (data.Columns.Contains(title))
                        {
                            data.Columns[title].ColumnName = file.ToString();
                        }
                        //header.AddField(title, file.ToString());
                        file++;
                    }
                }
            }
            var hash = new Hashtable();
            hash["data"] = data;
            hash["total"] = sqler.ExecuteScalar(countSQL, param);
            return hash;
        }

        protected override Hashtable GetHeader()
        {

            var header = new Header(this.ValueKey, 25);

            var match = System.Text.RegularExpressions.Regex.Match(this.SelectSQL, @"\sfrom\s", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            var index = match.Index;

            if (index == 0)
            {
                throw new ArgumentException("未配置正确的脚本");
            }
            var sel = this.SelectSQL.Substring(0, index).Split(',');
            char file = 'a';
            foreach (var s in sel)
            {
                var asindex = s.IndexOf(" as ", StringComparison.CurrentCultureIgnoreCase);
                if (asindex > 0)
                {
                    var title = s.Substring(asindex + 4).Trim();
                    if (String.Equals(this.ValueKey, title, StringComparison.CurrentCultureIgnoreCase) == false)
                    {
                        header.AddField(file.ToString(), title);
                        file++;
                    }
                }
            }
            return header.GetHeader();
        }
        public bool Check()
        {
            if (String.IsNullOrEmpty(this.SelectSQL))
            {
                return false;
            }

            var match = System.Text.RegularExpressions.Regex.Match(this.SelectSQL, @"\sfrom\s", System.Text.RegularExpressions.RegexOptions.IgnoreCase);


            if (match.Index == 0)
            {
                return false;
            }
            return true;
        }
    }


}
